﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Web;
using System.Collections;
using System.ComponentModel;


class httpPostData
{
	WebClient wc;
	List<string> querydata = new List<string>();
	string URL;

	public httpPostData(string URL)
	{
		this.URL = URL;
		
		IWebProxy proxy = WebRequest.GetSystemWebProxy();
		proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

		wc = new WebClient();
		wc.Proxy = proxy;
		wc.Credentials = proxy.Credentials;		
	}

	public void Add(string key, string val)
	{
		querydata.Add(key + " = " + HttpUtility.UrlEncode(val));
	}

	public string GetResponse()
	{
		/* ***************** PHP USAGE INSTRCUTIONS *****************
		 * If posting to a php script use the following code to get the post data
		 * 
		 * 	if (count($_POST) < 1){
		 *      // convert the raw POST data into the post array
		 *      $raw_data = file_get_contents( 'php://input' );  	
		 *      parse_str( $raw_data, $_POST );
		 *      unset($raw_data);
		 *  }
		 */
		string pars = string.Join("&", querydata.ToArray());

		byte[] b = wc.UploadData(this.URL, "POST", System.Text.Encoding.ASCII.GetBytes(pars));
		string sret = System.Text.Encoding.ASCII.GetString(b);

		return sret;
	}
}

